#!/usr/bin/perl
# IBM_PROLOG_BEGIN_TAG 
# This is an automatically generated prolog. 
#  
#  
#  
# Licensed Materials - Property of IBM 
#  
# Restricted Materials of IBM 
#  
# (C) COPYRIGHT International Business Machines Corp. 2002,2003 
# All Rights Reserved 
#  
# US Government Users Restricted Rights - Use, duplication or 
# disclosure restricted by GSA ADP Schedule Contract with IBM Corp. 
#  
# IBM_PROLOG_END_TAG 
####################################################################
#
#   lsswtopol command
#
#   Syntax:  lsswtopol {-n <network> -p <plane> [ -F <format> ] | --help }
#
#            -n <network>    - Specifies the network for displayed data
#            -p <plane>      - Specifies the plane for displayed data
#            --help          - Prints this message
#            -F <"format">   - a delimiter separated list of names
#                            representing desired properties to query.
#                            Valid values are:
#                                 frame
#                                 cage
#                                 power
#                                 chip
#                                 slot_riser
#                                 port
#                                 status
#                     If -F is not specified, all properties are shown.
#
####################################################################
#
#   Initial version - 09/06/02
#
####################################################################

use Getopt::Long;

sub bynumber { $a <=> $b; }

sub exit_routine {
    
     # remove files from /tmp 
     
     exit $ret; 

} # end exit_routine subroutine

################################################################################
#
# subroutines to parse properties desired (-F parm)
#
################################################################################
sub parse_props {
	$parm_length = length($props_parm);
	if (substr($props_parm,0,1) eq ":") {
                        # There was a colon at the beginning
			#      of the -F value
#			print "Colon in the beginning\n";   #DEBUG
			print $USAGE;
			$ret = 1;
			&exit_routine;
	}

	if (substr($props_parm,$parm_length-1) ne ":") {
                        # There was not a colon at the end
			#      of the -F value
#			print "No colon at the end\n";   #DEBUG
			print $USAGE;
			$ret = 1;
			&exit_routine;
	}

	# Find number of colons used
	$NUM_COLONS = 0;
	while ($props_parm =~ m/:/g) {
		$NUM_COLONS += 1;
	}

	@prop_list = split /:/, $props_parm, $MAX_PROPS + 1;

	# Now examine fields passed
        for ($i = 0; $i < $NUM_COLONS; $i++) {
		$prop = $prop_list[$i];
		# Were the fields specified properly?
		if ($prop eq "") {
#			print "There was a NULL field passed.\n";   #DEBUG
			print $USAGE;
			$ret = 1;
			&exit_routine;
		}
		else {
			# Is this a valid field name?
			$FOUND = 0;
			for ($j = 0; $j < $MAX_PROPS; $j++) {
				if ($prop eq $DEF_PROPS[$j]) {
					if ($FOUND_PROPS[$j] == 1) {
						# Second time for this prop
#						print "SECOND time\n"; #DEBUG
						print $USAGE;
						$ret = 1;
						&exit_routine;
					}
					else {
						$FOUND = 1;
						$FOUND_PROPS[$j] = 1;
						$ORDER[$NUM_PROPS] = $j;
					}
				}
			}

			# Was the field name a valid field name?
			if ($FOUND == 0) {
#				print "Field name NOT found\n";     #DEBUG
				print $USAGE;
				$ret = 1;
				&exit_routine;
			}

			$NUM_PROPS += 1;
			
#			# Were an incorrect number of fields passed?
#		        if ($NUM_PROPS > $MAX_PROPS) {
#				print "Too MANY props passed\n";        #DEBUG
#				print $USAGE;
#			}
		}
	}
}


################################################################################
#
# credentials subroutines
#
################################################################################
sub remove_creds {
        system "/bin/rm $RCMDTKFILE > /dev/null 2>&1";
        if ($DCE_tickets == 1) {
                `/bin/kdestroy`;
                 delete $ENV{'KRB5CCNAME'};
        }
} # end remove_creds subroutine

################################################################################
#
# start main body of code
#
################################################################################
$PRGRM  = "lsswtopol";
$BIN    = "/opt/hsc/bin"; 
$F_USED = 0;

$NUM_PROPS = 0;
$MAX_PROPS = 7;
@DEF_PROPS = ("frame" , "cage" , "power" , "chip" , "port" , "slot_riser" , "status" );
@FOUND_PROPS = (0, 0, 0, 0, 0, 0, 0);
#@HEAD = (" frame ", " cage ", " power ", " chip ", " slot_riser ", " port ", "      status ");
#@FORMAT = (" %5s ", " %4s ", " %5s ", " %4s ", " %10s ", " %4s ", " %7s ");
@HEAD = (" frame ", " cage ", " power ", " chip ", " port ", " slot_riser ", " status ");
@FORMAT = (" %5s ", " %4s ", " %5s ", " %4s ", " %4s ", " %10s "," %s ");

$USAGE = "Usage:  lsswtopol {-n <network> -p <plane> [ -F <format> ] | --help }\n";

# First check for existence of parameters
if ($#ARGV == -1) {
#	print "No parms\n";     #DEBUG
	print $USAGE;
	$ret = 1;
	&exit_routine;
}

# Get parameters used

Getopt::Long::Configure("no_ignore_case");
$result = GetOptions(
        "help" => \$help,
        "n=s" => \$net_parm,
        "p=s" => \$plane_parm,
        "F=s" => \$Format,
);

#print "GetOptions rc is ", $result, " .\n";

if ($result == 0) {
	# Bad result from GetOptions
#	print "Bad result from GetOptions\n";     #DEBUG
	print $USAGE;      #USAGE
	$ret = 1;
	&exit_routine;
}

if ($help) {
	# help was asked for
#	print "help was specified\n";     #DEBUG
	print $USAGE;      #USAGE
	$ret = 0;
	&exit_routine;
}

if (defined $net_parm) {
	if ($net_parm =~ /\D/) {
#		print "Bad -n value - not a number\n";     #DEBUG
		print $USAGE;
		$ret = 1;
		exit $ret;
	}
}
else {
	# No -n or nothing else was specified after -n
#	print "No -n value\n";     #DEBUG
	print $USAGE;
	$ret = 1;
	&exit_routine;
}

if (defined $plane_parm) {
	if ($plane_parm =~ /\D/) {
#		print "Bad -p value - not a number\n";     #DEBUG
		print $USAGE;
		$ret = 1;
		exit $ret;
	}
}
else {
	# No -p or nothing else was specified after -p
#	print "No -p value\n";     #DEBUG
	print $USAGE;
	$ret = 1;
	&exit_routine;
}

if (not defined $Format) {
	# -F was not specified, so use default
	$NUM_PROPS = $MAX_PROPS;
       	@FOUND_PROPS = (1, 1, 1, 1, 1, 1, 1);
       	@ORDER = (0, 1, 2, 3, 4, 5, 6);
}
else {
	$props_parm = $Format;  # colon-separated list of column names
	&parse_props;
}

# Now that we know what the issuer wants to see, let's get all the data
chop(@DATA = `CLASSPATH=/usr/websm/codebase/pluginjars/SNMGUI.jar:/usr/websm/codebase/wsm.jar:/usr/websm/codebase/pluginjars/hsc.jar java com.ibm.hsc.auiml.databeans.snm.SnmInfo swview -network $net_parm -plane $plane_parm`);
#chop(@DATA = `cd /opt/hsc/com/ibm/hsc/auiml/databeans/snm; CLASSPATH=/usr/websm/codebase/wsm.jar:/opt/hsc java com.ibm.hsc.auiml.databeans.snm.SnmInfo swview -network $net_parm -plane $plane_parm`);

if ($DATA[0] eq 0) {
#	print "SnmInfo returned 0.\n";             #DEBUG
	print "There is no data to display.\n";
	$ret = 1;
	&exit_routine;
}

$lines = 0;
foreach (@DATA) {
	($type, $f1, $f2, $f3, $f4, $f5, $f6) = split /\s+/;
	if ($type eq "BOARD_LINE") {
		$OUTPUT[$lines]{0} = "BOARD_LINE";
		$OUTPUT[$lines]{1} = $f1;   # frame
		$OUTPUT[$lines]{2} = $f2;   # cage
		$OUTPUT[$lines]{3} = $f3;   # power
		$OUTPUT[$lines]{4} = " ";   # chip
		$OUTPUT[$lines]{5} = " ";   # port
		$OUTPUT[$lines]{6} = " ";   # slot_riser
		$OUTPUT[$lines]{7} = $f4;   # status
	}
	elsif ($type eq "DEV_LINE") {
		$OUTPUT[$lines]{0} = "DEV_LINE";
		$OUTPUT[$lines]{1} = " ";   # frame
		$OUTPUT[$lines]{2} = " ";   # cage
		$OUTPUT[$lines]{3} = " ";   # power
		$OUTPUT[$lines]{4} = $f1;   # chip
		$OUTPUT[$lines]{5} = " ";   # port
		$OUTPUT[$lines]{6} = " ";   # slot_riser
		$OUTPUT[$lines]{7} = $f2;   # status
	}
	elsif ($type eq "PORT_LINE") {
		$OUTPUT[$lines]{0} = "PORT_LINE";
		$OUTPUT[$lines]{1} = " ";   # frame
		$OUTPUT[$lines]{2} = " ";   # cage
		$OUTPUT[$lines]{3} = " ";   # power
		$OUTPUT[$lines]{4} = " ";   # chip
		$OUTPUT[$lines]{5} = $f1;   # port
		$OUTPUT[$lines]{6} = $f2;   # slot_riser
		$OUTPUT[$lines]{7} = $f3." ".$f4." ".$f5." ".$f6;   # status
	}
	$lines += 1;
}

# If the user did not specify any parms, print out a header line
if (not defined $props_parm) {
	foreach (@ORDER) {
		$HEADER = $HEADER.$HEAD[$_];
	}
	print $HEADER."\n";
}

# Now for each line of data, we'll set up an output line with only the
#      data columns the user asked for
for ($k = 0; $k < $lines; $k++) {
	$LINE_OUT = "";      # Blank out output line

	# First see if we care about the data line - if it's a BOARD_LINE,
	#      we only care if one of the first three columns were asked for
	#      or if the last column was asked for
	if (($OUTPUT[$k]{0} eq "BOARD_LINE") && ($FOUND_PROPS[0] || $FOUND_PROPS[1] || $FOUND_PROPS[2] || $FOUND_PROPS[6])) {
		# Now according to the order of the fields requested,
		#     we'll generate the output line with each field's
		#     format and the data for that field
		foreach (@ORDER) {
			# If no parms were passed, we'll generate the output
			#     line with each field's format and data for that
			#     field;  otherwise, we'll just output the data
			#     separated by colons
			if (not defined $props_parm) {
				$FIELD_OUT = sprintf "$FORMAT[$_]", $OUTPUT[$k]{$_+1};
				$LINE_OUT = $LINE_OUT.$FIELD_OUT;
			}
			else {
				# Is there a value to print?
				if ($OUTPUT[$k]{$_+1} ne " ") {
					$LINE_OUT = $LINE_OUT.$OUTPUT[$k]{$_+1}.":";
				}
				else {
					# If not, just add a colon
					$LINE_OUT = $LINE_OUT.":";
				}
			}
		}
		print $LINE_OUT."\n";
	}
	# If it's a DEV_LINE, we only care if a DEV_LINE column 
	#      or if the last column was asked for
	elsif (($OUTPUT[$k]{0} eq "DEV_LINE") && ($FOUND_PROPS[3] || $FOUND_PROPS[6])) {
		# Now according to the order of the fields requested,
		#     we'll generate the output line with each field's
		#     format and the data for that field
		foreach (@ORDER) {
			# If no parms were passed, we'll generate the output
			#     line with each field's format and data for that
			#     field;  otherwise, we'll just output the data
			#     separated by colons
			if (not defined $props_parm) {
				$FIELD_OUT = sprintf "$FORMAT[$_]", $OUTPUT[$k]{$_+1};
				$LINE_OUT = $LINE_OUT.$FIELD_OUT;
			}
			else {
				# Is there a value to print?
				if ($OUTPUT[$k]{$_+1} ne " ") {
					$LINE_OUT = $LINE_OUT.$OUTPUT[$k]{$_+1}.":";
				}
				else {
					# If not, just add a colon
					$LINE_OUT = $LINE_OUT.":";
				}
			}
		}
		print $LINE_OUT."\n";
	}
	# If it's a PORT_LINE, we only care if a PORT_LINE column 
	#      or if the last column was asked for
	elsif (($OUTPUT[$k]{0} eq "PORT_LINE") && ($FOUND_PROPS[4] || $FOUND_PROPS[5] || $FOUND_PROPS[6])) {
		# Now according to the order of the fields requested,
		#     we'll generate the output line with each field's
		#     format and the data for that field
		foreach (@ORDER) {
			# If no parms were passed, we'll generate the output
			#     line with each field's format and data for that
			#     field;  otherwise, we'll just output the data
			#     separated by colons
			if (not defined $props_parm) {
				$FIELD_OUT = sprintf "$FORMAT[$_]", $OUTPUT[$k]{$_+1};
				$LINE_OUT = $LINE_OUT.$FIELD_OUT;
			}
			else {
				# Is there a value to print?
				if ($OUTPUT[$k]{$_+1} ne " ") {
					if ($DEF_PROPS[$_] eq "status") {
						# If this is for status, we
						#   need to use quotes
						$LINE_OUT = $LINE_OUT."\"".$OUTPUT[$k]{$_+1}."\":";
					}
					else {
						$LINE_OUT = $LINE_OUT.$OUTPUT[$k]{$_+1}.":";
					}
				}
				else {
					# If not, just add a colon
					$LINE_OUT = $LINE_OUT.":";
				}
			}
		}
		print $LINE_OUT."\n";
	}
}

$ret = 0;
$exit_routine;

